package com.qyhl.webtv.module_user.setting.cancel;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.google.gson.Gson;
import com.qyhl.webtv.basiclib.utils.DESedeUtil;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.basiclib.utils.network.model.ApiResult;
import com.qyhl.webtv.commonlib.constant.ServicePathConstant;
import com.qyhl.webtv.commonlib.service.ChannelConfigService;
import com.qyhl.webtv.module_user.common.UserUrl;

import java.util.Map;

public class UserCancelModel implements UserCancelContract.UserCancelModel {
    private UserCancelPresenter mPresenter;

    @Autowired(name = ServicePathConstant.GET_CONFIG_INFO)
    ChannelConfigService channelConfigService;

    UserCancelModel(UserCancelPresenter mPresenter) {
        this.mPresenter = mPresenter;
    }

    @Override
    public void getCode(String phone) {
        Map<String, String> map = DESedeUtil.encodeMapUrl(UserUrl.GET_USER_GETCODE);
        EasyHttp.post(map.get(DESedeUtil.URL))
                .upBytes(DESedeUtil.encryptMode(map.get(DESedeUtil.CODEKEY),
                        "siteId=" + channelConfigService.getSiteId() + "&type=5&telphone=" + phone))
                .isDecrypt(true)
                .decryptKet(map.get(DESedeUtil.CODEKEY))
                .execute(new SimpleCallBack<String>() {

                    @Override
                    public void onError(ApiException e) {
                        mPresenter.codeError("网络繁忙，请稍后再试");
                    }

                    @Override
                    public void onSuccess(String s) {
                        ApiResult apiResult = new Gson().fromJson(s,ApiResult.class);

                        if (apiResult.getCode() == 200) {
                            mPresenter.codeSuccess();
                        } else {
                            mPresenter.codeError("获取验证码失败," + apiResult.getMessage() + "！");
                        }
                    }
                });

    }

    @Override
    public void cancelAccount(String phone, String code) {
        Map<String, String> map = DESedeUtil.encodeMapUrl(UserUrl.POST_USER_CANCEL_ACCOUNT);

        EasyHttp.post(map.get(DESedeUtil.URL))
                .upBytes(DESedeUtil.encryptMode(map.get(DESedeUtil.CODEKEY),
                        "siteId=" + channelConfigService.getSiteId() + "&captcha=" + code + "&userName=" + phone))
                .isDecrypt(true)
                .decryptKet(map.get(DESedeUtil.CODEKEY))
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.cancelError("网络繁忙，请稍后再试");
                    }

                    @Override
                    public void onSuccess(String s) {
                        ApiResult apiResult = new Gson().fromJson(s,ApiResult.class);
                        if (apiResult.getCode() == 200) {
                            mPresenter.cancelSuccess();
                        } else {
                            mPresenter.cancelError("注销失败，" + apiResult.getMessage() + "！");
                        }
                    }
                });

    }
}
